package core;

/**
 * Class: Position
 * Responsibilities: Holding row and column for a piece. And translating a string to a position.
 * @author Tobias
 *
 */
public class Position {
	private int row;
	private int column;

	public Position(int c, int r) {
		row = r-1;
		column = c-1;
	}

	public Position() {

	}

	public int getRow() {
		return row;
	}
	public void setRow(int row) {
		this.row = row;
	}
	public int getColumn() {
		return column;
	}
	public void setColumn(int column) {
		this.column = column;
	}

	/**
	 * Given a string representation of a position (like "a2" or "B8"),
	 * this method returns a position-object with those coordinates.
	 * @param s
	 * @return
	 */
	public static Position parsePosition(String s) {
		s = s.toUpperCase();
		if(s.length() == 2) {
			Position p = new Position();
			p.setColumn((int)(s.charAt(0)-'A'));
			p.setRow((int)(Integer.parseInt(s.charAt(1)+"")-1));
			if(isOutsideBoard(p))
				return null;
			return p;
		}
		return null;
	}

	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Position){
			Position p = (Position) obj;
			if(p.getColumn() == column && p.getRow() == row)
				return true;
		}
		return false;
	}

	@Override
	public String toString() {
		return (char)(column + 'A') + ""  + (row + 1);
	}

	private static boolean isOutsideBoard(Position p){
		if((p.getColumn() < 0 || p.getColumn() >= 8) || (p.getRow() < 0 || p.getRow() >= 8))
			return false;
		return true;
	}

}
